package com.bossien.flink.common;

import com.bossien.common.utils.JsonUtils;
import com.bossien.flink.interfaces.IMessage;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;

/***
 * @author 胡春林
 *
 * 消息基础类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class BaseMessage<T> implements IMessage {

    //消息内容
    private T context;

    @Override
    public <T> T JsonToMessage(String jsonBody,Class<T> beanObj) throws RuntimeException,IOException {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            return objectMapper.readValue(jsonBody,beanObj);
    }

    @Override
    public String MessageToJson(Class message) { return JsonUtils.beanToJson(message); }

}
